<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Services\ThreeDMap\InfoSpotService;
use App\Services\ThreeDMap\MapService;
use App\Services\ThreeDMap\PanoramaService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MapController extends Controller
{
    protected $mapService;
    private PanoramaService $panoramaService;
    private InfoSpotService $infoSpotService;

    public function __construct(MapService $mapService,PanoramaService $panoramaService,InfoSpotService $infoSpotService)
    {
        $this->mapService = $mapService;
        $this->panoramaService = $panoramaService;
        $this->infoSpotService = $infoSpotService;
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function all(Request $request)
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->mapService->all($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
            'current' => 'required|integer',
            'pageSize' => 'required|integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->mapService->list($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 添加
     * @param Request $request
     * @return JsonResponse
     */
    public function add(Request $request)
    {
//        return $this->returnResponse(123);
        // 定义业务参数验证规则
        $paramRule = [
            'name' => 'required|string|min:2|max:255',
            'heard_panorama' => 'string',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);
        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }
//        return $this->returnResponse($params);
        $res = $this->mapService->add($params);

        if ($res['status']) {
            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 更新
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
            'name' => 'string|min:2|max:255',
            'heard_panorama' => 'string',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->mapService->update($params['guid'], $params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }


    /**
     * 删除
     * @param Request $request
     * @return JsonResponse
     */
    public function delete(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->mapService->delete($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }


    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function allPanorama(Request $request): JsonResponse
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'map_id' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->panoramaService->all($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }


    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function listPanorama(Request $request): JsonResponse
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'map_id' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
            'current' => 'required|integer',
            'pageSize' => 'required|integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->panoramaService->list($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 添加
     * @param Request $request
     * @return JsonResponse
     */
    public function addPanorama(Request $request)
    {
//        return $this->returnResponse(123);
        // 定义业务参数验证规则
        $paramRule = [
            'name' => 'required|string|min:2|max:255',
            'map_id' => 'required|string',
            'code_id' => 'required|string',
            'url' => 'required|string',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);
        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }
//        return $this->returnResponse($params);
        $res = $this->panoramaService->add($params);

        if ($res['status']) {
            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 更新
     * @param Request $request
     * @return JsonResponse
     */
    public function updatePanorama(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
            'name' => 'string|min:2|max:255',
            'map_id' => 'string',
            'url' => 'string',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->panoramaService->update($params['guid'], $params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }


    /**
     * 删除
     * @param Request $request
     * @return JsonResponse
     */
    public function deletePanorama(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->panoramaService->delete($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function allInfoSpot(Request $request): JsonResponse
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'panorama_id' => 'string|size:32',
            'to_panorama_id' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->infoSpotService->all($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function listInfoSpot(Request $request): JsonResponse
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string|size:32',
            'panorama_id' => 'string|size:32',
            'to_panorama_id' => 'string|size:32',
            'name' => 'string|max:255',
            'status' => 'integer',
            'current' => 'required|integer',
            'pageSize' => 'required|integer',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->infoSpotService->list($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 添加
     * @param Request $request
     * @return JsonResponse
     */
    public function addInfoSpot(Request $request)
    {
//        return $this->returnResponse(123);
        // 定义业务参数验证规则
        $paramRule = [
            'name' => 'required|string|min:2|max:255',
            'panorama_id' => 'required|string',
            'to_panorama_id' => 'string',
            'video' => 'array',
            'coordinates' => 'array',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);
        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }
//        return $this->returnResponse($params);
        $res = $this->infoSpotService->add($params);

        if ($res['status']) {
            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 更新
     * @param Request $request
     * @return JsonResponse
     */
    public function updateInfoSpot(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
            'name' => 'string|min:2|max:255',
            'panorama_id' => 'string',
            'to_panorama_id' => 'string',
            'video' => 'array',
            'coordinates' => 'array',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->infoSpotService->update($params['guid'], $params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }


    /**
     * 删除
     * @param Request $request
     * @return JsonResponse
     */
    public function deleteInfoSpot(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->infoSpotService->delete($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getMapStart(Request $request)
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->mapService->getMapStart($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getPanoramaInfo(Request $request)
    {

        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'string',
        ];

        // 验证参数
        $validateResult = $this->validateParam($request, $paramRule);

        // 根据验证结果确定是否返回响应还是获取业务参数
        if ($validateResult['status']) {
            // 获取验证后的业务参数
            $params = $validateResult['result'];
        } else {
            return $this->returnErrorResponse($validateResult['msg'], 40010);
        }

        $res = $this->panoramaService->getPanoramaInfo($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

}
